package com.uls.web.controller.ycl;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.ycl.domain.RepairPersonnel;
import com.uls.ycl.service.IRepairPersonnelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ycl.domain.RepairRecord;
import com.uls.ycl.service.IRepairRecordService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 维修记录Controller
 * 
 * @author ycl
 * @date 2024-11-16
 */
@RestController
@RequestMapping("/ycl/record")
public class RepairRecordController extends BaseController
{
    @Autowired
    private IRepairRecordService repairRecordService;

    @Autowired
    private IRepairPersonnelService repairPersonnelService;
    /**
     * 查询维修记录列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(RepairRecord repairRecord)
    {
        startPage();
        List<RepairRecord> list = repairRecordService.selectRepairRecordList(repairRecord);

        for (RepairRecord record:list){
            Long personnelId = record.getPersonnelId();
            RepairPersonnel repairPersonnel = repairPersonnelService.selectRepairPersonnelByPersonnelId(personnelId);
            if(repairPersonnel!=null)
                record.setName(repairPersonnel.getName());
        }

        return getDataTable(list);
    }

    /**
     * 导出维修记录列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:record:export')")
    @Log(title = "维修记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RepairRecord repairRecord)
    {
        List<RepairRecord> list = repairRecordService.selectRepairRecordList(repairRecord);
        ExcelUtil<RepairRecord> util = new ExcelUtil<RepairRecord>(RepairRecord.class);
        util.exportExcel(response, list, "维修记录数据");
    }

    /**
     * 获取维修记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ycl:record:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(repairRecordService.selectRepairRecordByRecordId(recordId));
    }

    /**
     * 新增维修记录
     */
    @PreAuthorize("@ss.hasPermi('ycl:record:add')")
    @Log(title = "维修记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepairRecord repairRecord)
    {
        return toAjax(repairRecordService.insertRepairRecord(repairRecord));
    }

    /**
     * 修改维修记录
     */
    @PreAuthorize("@ss.hasPermi('ycl:record:edit')")
    @Log(title = "维修记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepairRecord repairRecord)
    {
        return toAjax(repairRecordService.updateRepairRecord(repairRecord));
    }

    /**
     * 删除维修记录
     */
    @PreAuthorize("@ss.hasPermi('ycl:record:remove')")
    @Log(title = "维修记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(repairRecordService.deleteRepairRecordByRecordIds(recordIds));
    }
}
